package com.java.model.singleton;

import java.io.Serializable;

/**
 * 饿汉模式
 * @author wangning
 * @create 2021-04-20 14:36
 */
public class HungrySingleton implements Serializable {
	//饿汉模式
	//缺点：没有使用懒加载，可能会导致内存浪费
	private static final HungrySingleton INSTANCE = new HungrySingleton();
	private HungrySingleton() {
		if(INSTANCE != null) {
			throw new RuntimeException("单例对象不能重复创建");//这种能够解决通过反射破坏单例的方式
		}
	}
	public static HungrySingleton getInstance() {
		return INSTANCE;
	}
	public static void otherMethod() {
		System.out.println("otherMethod");
	}

	//阻止反序列化破坏单例
	public Object readResolve() {
		return INSTANCE;
	}
}
